package com.codebattery.controller;

import com.alibaba.fastjson.JSONObject;
import com.codebattery.config.RabbitMQConfig;
import com.codebattery.domain.entity.Goods;
import com.codebattery.domain.model.*;
import com.codebattery.service.GoodsService;
import com.rabbitmq.client.Channel;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Optional;


/**
 * 商品服务
 */
@RestController
@RequestMapping("/goods")
public class GoodsController {
    private static final Logger logger = LoggerFactory.getLogger(GoodsController.class);

    @Value("${oss.endpoint}")
    private String endpoint;
    @Value("${oss.accessKeyId}")
    private String accessKeyId;
    @Value("${oss.accessKeySecret}")
    private String accessKeySecret;
    @Value("${oss.bucketName}")
    private String bucketName;

    @Resource
    private GoodsService goodsService;

    @ApiOperation(value="添加商品",notes="")
    @RequestMapping(value = "/test", method = RequestMethod.GET, produces = "application/json")
    public String addGoods() throws Exception {
        return null;
    }

    @ApiOperation(value="查看商品详情",notes="")
    @RequestMapping(value = "/get-goods/{goodsId}", method = RequestMethod.GET, produces = "application/json")
    public Goods getGoods(@PathVariable Integer goodsId) {
        return goodsService.getGoods(goodsId);
    }

    @ApiOperation(value="查看商品详情",notes="")
    @RequestMapping(value = "/detail/{goodId}", method = RequestMethod.POST, produces = "application/json")
    public GoodsInfo detail(@PathVariable Integer goodId) throws Exception {


        return goodsService.detail(goodId);
    }

    @ApiOperation(value="搜索商品",notes="")
    @RequestMapping(value = "/search", method = RequestMethod.POST, produces = "application/json")
    public GoodsListSearchInfo search(@RequestBody Map<String, String> param) throws Exception {

        String query = Optional.ofNullable(param.get("query")).orElse(null);

        return goodsService.seach(query);
    }

    @ApiOperation(value="查询商品",notes="")
    @RequestMapping(value = "/list", method = RequestMethod.POST, produces = "application/json")
    public List<SeckillGoodsInfo> list(@RequestBody Map param) throws Exception {
        return goodsService.listSeckillGoods();
    }


    /**
     * 监听订单消息队列，并消费
     *
     */
    @RabbitListener(queues = RabbitMQConfig.GOODS_QUEUE)
    public void createGoods(@Payload JSONObject jsonObject, Channel channel, @Header(AmqpHeaders.DELIVERY_TAG) long tag) throws Exception {

        logger.info("收到商品消息[{}]", jsonObject);
        StoreProduct product = (StoreProduct) JSONObject.toJavaObject(jsonObject, StoreProduct.class);

        /**
         * 调用数据库orderService创建订单信息
         */
        goodsService.addGoods(product);

        channel.basicAck(tag, false);
    }
}
